public class sortList {  public ListNode sortList(ListNode head) {
    if(head == null || head.next == null){
        return head;
    }
    ListNode mid = getmid(head);
    ListNode left = head;
    ListNode right = mid.next;
    mid.next = null;

    left = sortList(left);
    right = sortList(right);

    return merge(left,right);


}
    public ListNode getmid(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    public ListNode merge(ListNode l1,ListNode l2){
        ListNode dummy = new ListNode(0);
        ListNode tail = dummy;
        while(l1 != null && l2 != null){
            if(l1.val < l2.val){
                tail.next = l1;
                l1 = l1.next;
            }else {
                tail.next = l2;
                l2 = l2.next;
            }
            tail = tail.next;
        }
        tail.next =(l1 != null) ? l1 : l2;
        return dummy.next;
    }
}
